package com.bum.rss.service.helper;

import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.bum.rss.bo.SiteBO;
import com.bum.rss.exp.om.Rss;

public class PreloadSite implements ApplicationContextAware {
	final Logger	log	= Logger.getLogger(getClass());

	SiteBO			siteBO;

	private String	site;
	private long	id	= 12675;

	@Override
	public void setApplicationContext(ApplicationContext arg0) throws BeansException {
		Executors.newScheduledThreadPool(1).scheduleWithFixedDelay(new Runnable() {
			@Override
			public void run() {
				try {
					if (--id > 0) {
						log.debug("Load " + id);
						Rss rss = siteBO.load(site + "/m/" + id, null);
						if (rss != null && rss.getEntry() != null) {
							System.out.println(rss.getEntry().getTitle());
						}
					}
				} catch (Throwable e) {
					log.debug(e.getMessage(), e);
				}
			}
		}, 1000, 100, TimeUnit.MILLISECONDS);
	}

	public void setSiteBO(SiteBO siteBO) {
		this.siteBO = siteBO;
	}

	public void setId(long id) {
		this.id = id;
	}

	public void setSite(String site) {
		this.site = site;
	}

}
